package com.naiterui.ehp.bs.doctor.dao.mongo;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import org.slf4j.LoggerFactory;

import com.naiterui.common.repository.page.Page;
import com.naiterui.ehp.bs.doctor.prodocl.DrBatchMessage;

@Repository
public class AdDrBatchMessageDao extends MongoBaseDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdDrBatchMessageDao.class);

    private static final String COLLECTION_NAME = "dr_batch_message";

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    protected MongoTemplate getMongoTemplate() {
        return this.mongoTemplate;
    }

    public Long getCountDrBathcMessage(Long doctorId) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date todayStart = calendar.getTime();
        calendar.add(Calendar.DATE, 1);
        Date endStart = calendar.getTime();
        Criteria criteria = Criteria.where("doctorId").is(doctorId).and("timeStamp").gte(todayStart.getTime()).lte(endStart.getTime());
        LOGGER.info("todayStart:{},endStart:{}", todayStart.getTime(), endStart.getTime());
        Query query = new Query();
        query.addCriteria(criteria);
        return super.queryCount(query, COLLECTION_NAME);
    }

    public void save(DrBatchMessage drBatchMessage) {
        super.save(drBatchMessage, COLLECTION_NAME);
    }

    public List<DrBatchMessage> getDrBatchMessageList(Long doctorId, int pageNo, int pageSize) {
        Criteria criteria = Criteria.where("doctorId").is(doctorId).and("status").is(0);
        return super.queryByPage(COLLECTION_NAME, DrBatchMessage.class, criteria, pageNo, pageSize, Page.DESC, "timeStamp");
    }

    public Long queryCount(Long doctorId) {
        Criteria criteria = Criteria.where("doctorId").is(doctorId).and("status").is(0);
        Query query = new Query();
        query.addCriteria(criteria);
        return super.queryCount(query, COLLECTION_NAME);
    }

    public DrBatchMessage getDrBatchMessageById(String id) {
        Criteria criteria = Criteria.where("id").is(id);
        Query query = Query.query(criteria);
        List<DrBatchMessage> results = super.find(query, DrBatchMessage.class, COLLECTION_NAME);
        return results.size() > 0 ? results.get(0) : null;
    }

    public void update(DrBatchMessage drBatchMessage) {
        if (null != drBatchMessage && null != drBatchMessage.getId()) {
            Criteria criteria = Criteria.where("id").is(drBatchMessage.getId());
            Query query = Query.query(criteria);
            super.remove(query, COLLECTION_NAME);
            super.save(drBatchMessage, COLLECTION_NAME);
        }
    }

    public List<DrBatchMessage> findDrBatchMessageByMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.MONTH, -3);

        Date endDate = calendar.getTime();
        calendar.add(Calendar.DATE, -1);
        Date startDate = calendar.getTime();
        LOGGER.info("DrMessageController methon removeTask startDate:{},endDate:{}", startDate.getTime(), endDate.getTime());
        Criteria criteria = Criteria.where("timeStamp").gte(startDate.getTime()).lte(endDate.getTime()).and("status").is(0);
        Query query = new Query();
        query.addCriteria(criteria);
        return super.find(query, DrBatchMessage.class, COLLECTION_NAME);
    }

}
